package com.mvp.lt.airlineview.view;

import android.content.ClipData;
import android.content.Context;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.annotation.Nullable;

import com.android.library.utils.ToastUtils;
import com.mvp.lt.airlineview.R;
import com.mvp.lt.airlineview.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LiuTao on 2019/12/31 0031 下午 15:15
 * functiona:
 */
public class RemoteCustomControlView extends LinearLayout {
    @BindView(R.id.top_view1_container_rising)
    LinearLayout topView1ContainerRising;
    @BindView(R.id.top_view1_container_falling)
    LinearLayout topView1ContainerFalling;
    @BindView(R.id.top_view_container_toLeft)
    LinearLayout topViewContainerToLeft;
    @BindView(R.id.top_view_container_toRight)
    LinearLayout topViewContainerToRight;
    @BindView(R.id.top_view_container_forward)
    LinearLayout topViewContainerForward;
    @BindView(R.id.top_view_container_back)
    LinearLayout topViewContainerBack;
    @BindView(R.id.top_view_container_turnLeft)
    LinearLayout topViewContainerTurnLeft;
    @BindView(R.id.top_view_container_turnRight)
    LinearLayout topViewContainerTurnRight;
    @BindView(R.id.view1_rising)
    LinearLayout view1Rising;
    @BindView(R.id.view1_falling)
    LinearLayout view1Falling;
    @BindView(R.id.view_toLeft)
    LinearLayout viewToLeft;
    @BindView(R.id.view_toRight)
    LinearLayout viewToRight;
    @BindView(R.id.view_forward)
    LinearLayout viewForward;
    @BindView(R.id.view_back)
    LinearLayout viewBack;
    @BindView(R.id.view_turnLeft)
    LinearLayout viewTurnLeft;
    @BindView(R.id.view_turnRight)
    LinearLayout viewTurnRight;
    @BindView(R.id.left_view_left_container)
    LinearLayout leftViewLeftContainer;
    @BindView(R.id.left_view_right_container)
    LinearLayout leftViewRightContainer;
    @BindView(R.id.left_view_top_container)
    LinearLayout leftViewTopContainer;
    @BindView(R.id.left_view_bottom_container)
    LinearLayout leftViewBottomContainer;
    @BindView(R.id.right_view_left_container)
    LinearLayout rightViewLeftContainer;
    @BindView(R.id.right_view_right_container)
    LinearLayout rightViewRightContainer;
    @BindView(R.id.right_view_top_container)
    LinearLayout rightViewTopContainer;
    @BindView(R.id.right_view_bottom_container)
    LinearLayout rightViewBottomContainer;
    @BindView(R.id.set_map_build)
    TextView setMapBuild;
    @BindView(R.id.rc_back)
    ImageView rcBack;

    private String TAG = "RCDragViewActivity";

    private final String TAG_RISING = "rc_rising";
    private final String TAG_FALLING = "rc_falling";
    private final String TAG_TOLEFT = "rc_toLeft";
    private final String TAG_TORIGHT = "rc_toRight";
    private final String TAG_FORWARD = "rc_forward";
    private final String TAG_BACK = "rc_back";
    private final String TAG_TURNLEFT = "rc_turnLeft";
    private final String TAG_TURNRIGHT = "rc_turnRight";

//    private AircraftStickMapping.Builder leftVerticalBuilder;
//    private AircraftStickMapping.Builder leftHorizontalBuilder;
//    private AircraftStickMapping.Builder rightVerticalBuilder;
//    private AircraftStickMapping.Builder rightHorizontalBuilder;

    public RemoteCustomControlView(Context context) {
        super(context);
        initView(context);
    }

    public RemoteCustomControlView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public RemoteCustomControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context) {
        initAircraftStickMappingBuilder();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_remote_drag_view, this, true);
        ButterKnife.bind(this, view);
        view1Rising.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("1", "1"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });
        view1Falling.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("2", "2"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });
        viewToLeft.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("3", "3"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });
        viewToRight.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("4", "4"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);

                return false;
            }
        });
        viewForward.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("5", "5"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });
        viewBack.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("6", "6"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });
        viewTurnLeft.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("7", "7"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);

                return false;
            }
        });
        viewTurnRight.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                ClipData clipData = new ClipData(ClipData.newPlainText("8", "8"));
                DragShadowBuilder myShadow = new DragShadowBuilder(view);
                view.startDrag(clipData, myShadow, view, 0);
                return false;
            }
        });

        OnDragListener topDragListensr = new OnDragListener() {
            @Override
            public boolean onDrag(View view, DragEvent dragEvent) {
                switch (dragEvent.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:

                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:

                        break;
                    case DragEvent.ACTION_DRAG_LOCATION:

                        break;
                    case DragEvent.ACTION_DROP:
                        try {
                            ViewGroup thisContainer = ((ViewGroup) view);
                            View draggingView = (View) dragEvent.getLocalState();
                            ViewGroup draggingViewContainer = null;
                            String tag = null;
                            if (draggingView != null) {
                                draggingViewContainer = (ViewGroup) draggingView.getParent();
                                tag = (String) ((ViewGroup) draggingView).getTag();
                            }
                            if (draggingViewContainer != null) {
                                if (thisContainer == topView1ContainerRising
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_RISING.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);//
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);
                                    } else if (TAG_FALLING.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topView1ContainerFalling.addView(draggingView);//
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topView1ContainerFalling, draggingViewContainer);
                                    }
                                } else if (thisContainer == topView1ContainerFalling
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_FALLING.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);
                                    } else if (TAG_RISING.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topView1ContainerRising.addView(draggingView);//
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topView1ContainerRising, draggingViewContainer);
                                    }
                                } else if (thisContainer == topViewContainerToLeft
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_TOLEFT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);

                                    } else if (TAG_TORIGHT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerToRight.addView(draggingView);//
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerToRight, draggingViewContainer);
                                    }
                                } else if (thisContainer == topViewContainerToRight
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_TORIGHT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);

                                    } else if (TAG_TOLEFT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerToLeft.addView(draggingView);//
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerToLeft, draggingViewContainer);
                                    }
                                } else if (thisContainer == topViewContainerForward
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_FORWARD.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);

                                    } else if (TAG_BACK.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerBack.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerBack, draggingViewContainer);

                                    }
                                } else if (thisContainer == topViewContainerBack
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_BACK.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);

                                    } else if (TAG_FORWARD.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerForward.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerForward, draggingViewContainer);

                                    }
                                } else if (thisContainer == topViewContainerTurnLeft
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_TURNLEFT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);
                                    } else if (TAG_TURNRIGHT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerTurnRight.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerTurnRight, draggingViewContainer);
                                    }
                                } else if (thisContainer == topViewContainerTurnRight
                                        && draggingViewContainer != thisContainer) {
                                    if (TAG_TURNRIGHT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        thisContainer.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(thisContainer, draggingViewContainer);

                                    } else if (TAG_TURNLEFT.equals(tag)) {
                                        draggingViewContainer.removeView(draggingView);
                                        topViewContainerTurnLeft.addView(draggingView);
                                        draggingViewContainer.addView(getChild_view());
                                        adpterTopContainerView(topViewContainerTurnLeft, draggingViewContainer);

                                    }
                                }
                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        break;
                    default:
                        break;
                }
                return true;
            }
        };

        OnDragListener bottomDragListensr = new OnDragListener() {
            @Override
            public boolean onDrag(View view, DragEvent dragEvent) {
                switch (dragEvent.getAction()) {
                    case DragEvent.ACTION_DRAG_STARTED:
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
                    case DragEvent.ACTION_DRAG_LOCATION:
                        break;
                    case DragEvent.ACTION_DROP:
                        try {
                            View draggingView = (View) dragEvent.getLocalState();
                            ViewGroup draggingViewContainer = null;
                            if (draggingView != null) {
                                draggingViewContainer = (ViewGroup) draggingView.getParent();
                            }
                            ViewGroup thisContainer = ((ViewGroup) view);
                            View coveredView = thisContainer.getChildAt(0);
                            if (draggingViewContainer != null && (draggingViewContainer == topView1ContainerRising
                                    || draggingViewContainer == topView1ContainerFalling
                                    || draggingViewContainer == topViewContainerToLeft
                                    || draggingViewContainer == topViewContainerToRight
                                    || draggingViewContainer == topViewContainerForward
                                    || draggingViewContainer == topViewContainerBack
                                    || draggingViewContainer == topViewContainerTurnLeft
                                    || draggingViewContainer == topViewContainerTurnRight)) {
                                String tag = "";
                                if (coveredView != null) {
                                    tag = (String) coveredView.getTag();
                                }
                                if (draggingView != coveredView) {
                                    thisContainer.removeView(coveredView);
                                    draggingViewContainer.removeView(draggingView);
                                    thisContainer.addView(draggingView);
                                    View contencView = adapterTopContainerView(draggingViewContainer, thisContainer);
                                    if (TAG_RISING.equals(tag)) {
                                        topView1ContainerRising.addView(coveredView);
                                        if (contencView != null) {
                                            topView1ContainerFalling.addView(contencView);
                                        }
                                    } else if (TAG_FALLING.equals(tag)) {
                                        topView1ContainerFalling.addView(coveredView);
                                        if (contencView != null) {
                                            topView1ContainerRising.addView(contencView);
                                        }
                                    } else if (TAG_TOLEFT.equals(tag)) {
                                        topViewContainerToLeft.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerToRight.addView(contencView);
                                        }
                                    } else if (TAG_TORIGHT.equals(tag)) {
                                        topViewContainerToRight.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerToLeft.addView(contencView);
                                        }
                                    } else if (TAG_FORWARD.equals(tag)) {
                                        topViewContainerForward.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerBack.addView(contencView);
                                        }
                                    } else if (TAG_BACK.equals(tag)) {
                                        topViewContainerBack.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerForward.addView(contencView);
                                        }
                                    } else if (TAG_TURNLEFT.equals(tag)) {
                                        topViewContainerTurnLeft.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerTurnRight.addView(contencView);
                                        }
                                    } else if (TAG_TURNRIGHT.equals(tag)) {
                                        topViewContainerTurnRight.addView(coveredView);
                                        if (contencView != null) {
                                            topViewContainerTurnLeft.addView(contencView);
                                        }
                                    }
                                }
                            } else {
                                if (draggingView != coveredView && draggingViewContainer != null) {
                                    thisContainer.removeView(coveredView);
                                    draggingViewContainer.removeView(draggingView);
                                    thisContainer.addView(draggingView);
                                    draggingViewContainer.addView(coveredView);
                                    ViewGroup draggingContainerView2 = getContainer(draggingViewContainer);
                                    ViewGroup thisContainerView2 = getContainer(thisContainer);
                                    if (draggingContainerView2 != thisContainer
                                            && draggingContainerView2 != null
                                            && thisContainerView2 != null) {
                                        View draggingView2 = draggingContainerView2.getChildAt(0);
                                        View thisView2 = thisContainerView2.getChildAt(0);
                                        draggingContainerView2.removeView(draggingView2);
                                        thisContainerView2.removeView(thisView2);
                                        draggingContainerView2.addView(thisView2);
                                        thisContainerView2.addView(draggingView2);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        break;
                    default:
                        break;
                }
                return true;
            }
        };
        topView1ContainerRising.setOnDragListener(topDragListensr);
        topView1ContainerFalling.setOnDragListener(topDragListensr);
        topViewContainerToLeft.setOnDragListener(topDragListensr);
        topViewContainerToRight.setOnDragListener(topDragListensr);
        topViewContainerForward.setOnDragListener(topDragListensr);
        topViewContainerBack.setOnDragListener(topDragListensr);
        topViewContainerTurnLeft.setOnDragListener(topDragListensr);
        topViewContainerTurnRight.setOnDragListener(topDragListensr);

        leftViewLeftContainer.setOnDragListener(bottomDragListensr);
        leftViewRightContainer.setOnDragListener(bottomDragListensr);
        leftViewTopContainer.setOnDragListener(bottomDragListensr);
        leftViewBottomContainer.setOnDragListener(bottomDragListensr);
        rightViewLeftContainer.setOnDragListener(bottomDragListensr);
        rightViewRightContainer.setOnDragListener(bottomDragListensr);
        rightViewTopContainer.setOnDragListener(bottomDragListensr);
        rightViewBottomContainer.setOnDragListener(bottomDragListensr);

        setMapBuild.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setLeftAndRightRemoteControlListener();
                setCustomAircraftMapping();
            }
        });
        rcBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
            }
        });
    }


    private void adpterTopContainerView(ViewGroup thisContainer, ViewGroup draggingViewContainer) {
        ViewGroup draggingContainerView2 = getContainer(draggingViewContainer);
        ViewGroup thisContainerView2 = getTopContainer(thisContainer);
        if (draggingContainerView2 != null && thisContainerView2 != null) {
            View draggingView2 = draggingContainerView2.getChildAt(0);
            View thisView2 = thisContainerView2.getChildAt(0);
            draggingContainerView2.removeView(draggingView2);
            thisContainerView2.removeView(thisView2);
            draggingContainerView2.addView(getChild_view());
            thisContainerView2.addView(draggingView2);
        }
    }

    private ViewGroup getTopContainer(ViewGroup thisContainer) {
        ViewGroup viewContainer = null;
        if (thisContainer == topView1ContainerRising) {//view1Rising
            viewContainer = topView1ContainerFalling;
        } else if (thisContainer == topView1ContainerFalling) {
            viewContainer = topView1ContainerRising;
        } else if (thisContainer == topViewContainerToLeft) {
            viewContainer = topViewContainerToRight;
        } else if (thisContainer == topViewContainerToRight) {
            viewContainer = topViewContainerToLeft;
        } else if (thisContainer == topViewContainerForward) {
            viewContainer = topViewContainerBack;
        } else if (thisContainer == topViewContainerBack) {
            viewContainer = topViewContainerForward;
        } else if (thisContainer == topViewContainerTurnLeft) {
            viewContainer = topViewContainerTurnRight;
        } else if (thisContainer == topViewContainerTurnRight) {
            viewContainer = topViewContainerTurnLeft;
        }
        return viewContainer;
    }

    private ViewGroup getContainer(ViewGroup thisViewGroup) {
        ViewGroup viewContainer = null;
        if (thisViewGroup == leftViewLeftContainer) {
            viewContainer = leftViewRightContainer;
        } else if (thisViewGroup == leftViewRightContainer) {
            viewContainer = leftViewLeftContainer;
        } else if (thisViewGroup == leftViewTopContainer) {
            viewContainer = leftViewBottomContainer;
        } else if (thisViewGroup == leftViewBottomContainer) {
            viewContainer = leftViewTopContainer;
        } else if (thisViewGroup == rightViewLeftContainer) {
            viewContainer = rightViewRightContainer;
        } else if (thisViewGroup == rightViewRightContainer) {
            viewContainer = rightViewLeftContainer;
        } else if (thisViewGroup == rightViewTopContainer) {
            viewContainer = rightViewBottomContainer;
        } else if (thisViewGroup == rightViewBottomContainer) {
            viewContainer = rightViewTopContainer;
        }
        return viewContainer;
    }

    private View adapterTopContainerView(ViewGroup draggingViewContainer, ViewGroup thisContainer) {
        if (draggingViewContainer == topView1ContainerRising) {//view1Rising
            View coveredView = topView1ContainerFalling.getChildAt(0);
            topView1ContainerFalling.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topView1ContainerFalling) {
            View coveredView = topView1ContainerRising.getChildAt(0);
            topView1ContainerRising.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerToLeft) {
            View coveredView = topViewContainerToRight.getChildAt(0);
            topViewContainerToRight.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerToRight) {
            View coveredView = topViewContainerToLeft.getChildAt(0);
            topViewContainerToLeft.removeView(coveredView);
            adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerForward) {
            View coveredView = topViewContainerBack.getChildAt(0);
            topViewContainerBack.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerBack) {
            View coveredView = topViewContainerForward.getChildAt(0);
            topViewContainerForward.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerTurnLeft) {
            View coveredView = topViewContainerTurnRight.getChildAt(0);
            topViewContainerTurnRight.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        } else if (draggingViewContainer == topViewContainerTurnRight) {
            View coveredView = topViewContainerTurnLeft.getChildAt(0);
            topViewContainerTurnLeft.removeView(coveredView);
            return adapterBottomContainerView(thisContainer, coveredView);
        }
        return null;
    }

    private View adapterBottomContainerView(ViewGroup thisContainer, View view) {
        View coveredView = null;
        if (thisContainer == leftViewLeftContainer) {
            coveredView = leftViewRightContainer.getChildAt(0);
            leftViewRightContainer.removeView(coveredView);
            leftViewRightContainer.addView(view);
        } else if (thisContainer == leftViewRightContainer) {
            coveredView = leftViewLeftContainer.getChildAt(0);
            leftViewLeftContainer.removeView(coveredView);
            leftViewLeftContainer.addView(view);
        } else if (thisContainer == leftViewTopContainer) {
            coveredView = leftViewBottomContainer.getChildAt(0);
            leftViewBottomContainer.removeView(coveredView);
            leftViewBottomContainer.addView(view);
        } else if (thisContainer == leftViewBottomContainer) {
            coveredView = leftViewTopContainer.getChildAt(0);
            leftViewTopContainer.removeView(coveredView);
            leftViewTopContainer.addView(view);
        } else if (thisContainer == rightViewLeftContainer) {
            coveredView = rightViewRightContainer.getChildAt(0);
            rightViewRightContainer.removeView(coveredView);
            rightViewRightContainer.addView(view);
        } else if (thisContainer == rightViewRightContainer) {
            coveredView = rightViewLeftContainer.getChildAt(0);
            rightViewLeftContainer.removeView(coveredView);
            rightViewLeftContainer.addView(view);
        } else if (thisContainer == rightViewTopContainer) {
            coveredView = rightViewBottomContainer.getChildAt(0);
            rightViewBottomContainer.removeView(coveredView);
            rightViewBottomContainer.addView(view);
        } else if (thisContainer == rightViewBottomContainer) {
            coveredView = rightViewTopContainer.getChildAt(0);
            rightViewTopContainer.removeView(coveredView);
            rightViewTopContainer.addView(view);
        }
        return coveredView;
    }


    public ViewGroup getChild_view() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        ImageView textView = new ImageView(getContext());
        linearLayout.addView(textView, UIUtils.dip2Px(46), UIUtils.dip2Px(46));
        return linearLayout;
    }


    public void initAircraftStickMappingBuilder() {
//        leftVerticalBuilder = new AircraftStickMapping.Builder();
//        leftHorizontalBuilder = new AircraftStickMapping.Builder();
//        rightVerticalBuilder = new AircraftStickMapping.Builder();
//        rightHorizontalBuilder = new AircraftStickMapping.Builder();
    }


    private boolean isLeftHorizontalBuilder = false;
    private boolean isLeftVerticalBuilder = false;

    private boolean isRightHorizontalBuilder = false;
    private boolean isRightVerticalBuilder = false;

    public void setLeftAndRightRemoteControlListener() {
        View leftViewLeftcoveredView = leftViewLeftContainer.getChildAt(0);
        if (leftViewLeftcoveredView != null) {
            String tag = (String) leftViewLeftcoveredView.getTag();
//            isLeftHorizontalBuilder = setMapBuilder(leftHorizontalBuilder, tag);
            isLeftHorizontalBuilder = true;
        } else {
            isLeftHorizontalBuilder = false;
        }

        View leftViewTopcoveredView = leftViewTopContainer.getChildAt(0);
        if (leftViewTopcoveredView != null) {
            String tag = (String) leftViewTopcoveredView.getTag();
//            isLeftVerticalBuilder = setMapBuilder(leftVerticalBuilder, tag);
            isLeftVerticalBuilder = true;
        } else {
            isLeftVerticalBuilder = false;
        }
        View rightViewLeftcoveredView = rightViewLeftContainer.getChildAt(0);
        if (rightViewLeftcoveredView != null) {
            String tag = (String) rightViewLeftcoveredView.getTag();
//            isRightHorizontalBuilder = setMapBuilder(rightHorizontalBuilder, tag);
            isRightHorizontalBuilder = true;
        } else {
            isRightHorizontalBuilder = false;
        }
        View rightViewTopcoveredView = rightViewTopContainer.getChildAt(0);
        if (rightViewTopcoveredView != null) {
            String tag = (String) rightViewTopcoveredView.getTag();
//            isRightVerticalBuilder = setMapBuilder(rightVerticalBuilder, tag);
            isRightVerticalBuilder = true;
        } else {
            isRightVerticalBuilder = false;
        }
    }

//    public boolean setMapBuilder(AircraftStickMapping.Builder builder, String tag) {
//        boolean isSetBuilder = true;
//        if (TAG_RISING.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.THROTTLE);
//            builder.isReversed(false);
//        } else if (TAG_FALLING.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.THROTTLE);
//            builder.isReversed(true);
//        } else if (TAG_TOLEFT.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.ROLL);
//            builder.isReversed(false);
//        } else if (TAG_TORIGHT.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.ROLL);
//            builder.isReversed(true);
//        } else if (TAG_FORWARD.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.PITCH);
//            builder.isReversed(false);
//        } else if (TAG_BACK.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.PITCH);
//            builder.isReversed(true);
//        } else if (TAG_TURNLEFT.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.YAW);
//            builder.isReversed(false);
//        } else if (TAG_TURNRIGHT.equals(tag)) {
//            builder.mappingTarget(AircraftStickMappingTarget.YAW);
//            builder.isReversed(true);
//        } else {
//            isSetBuilder = false;
//        }
//        return isSetBuilder;
//    }


    public void setCustomAircraftMapping() {
        if (isLeftHorizontalBuilder
                && isLeftVerticalBuilder
                && isRightHorizontalBuilder
                && isRightVerticalBuilder) {
//            AircraftMapping aircraftMapping = new AircraftMapping(leftVerticalBuilder.build(), leftHorizontalBuilder.build(), rightVerticalBuilder.build(), rightHorizontalBuilder.build());
//            if (DJIModuleVerificationUtil.isRemoteControllerAvailable()) {
//                Aircraft aircraft = (Aircraft) DJISDKManager.getInstance().getProduct();
//                aircraft.getRemoteController().setCustomAircraftMapping(aircraftMapping,
//                        new CommonCallbacks.CompletionCallback() {
//                            @Override
//                            public void onResult(DJIError djiError) {
//                                if (djiError == null) {
//                                    ToastUtils.showToast("设置成功");
//                                }
//                            }
//                        });
//            } else {
//                ToastUtils.showToast("遥控器未连接！");
//            }
        } else {
            ToastUtils.showToast("请完成自定义遥控控制！");
        }

    }

}
